package org.jeecg.modules.mes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.mes.entity.MesWorkType;
import org.jeecg.modules.mes.service.IMesWorkTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工种管理
 */
@Slf4j
@Api(tags = "工种管理")
@RestController
@RequestMapping("/mes/worktype")
public class MesWorkTypeController extends JeecgController<MesWorkType, IMesWorkTypeService> {

    @Autowired
    private IMesWorkTypeService mesWorkTypeService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "工种管理-分页列表查询")
    @ApiOperation(value = "工种管理-分页列表查询", notes = "工种管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MesWorkType>> queryPageList(MesWorkType mesWorkType,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        // 创建分页对象
        Page<MesWorkType> page = new Page<>(pageNo, pageSize);
        
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        
        // 添加查询条件
        params.put("workTypeCode", req.getParameter("workTypeCode_like"));
        params.put("workTypeName", req.getParameter("workTypeName_like"));
        params.put("status", req.getParameter("status"));
        
        // 调用Service查询
        IPage<MesWorkType> pageList = mesWorkTypeService.queryPageList(page, params);
        
        return Result.OK(pageList);
    }

    /**
     * 添加
     */
    @AutoLog(value = "工种管理-添加")
    @ApiOperation(value = "工种管理-添加", notes = "工种管理-添加")
    @PostMapping(value = "/add")
    public Result<MesWorkType> add(@RequestBody MesWorkType mesWorkType) {
        return mesWorkTypeService.saveWorkType(mesWorkType);
    }

    /**
     * 编辑
     */
    @AutoLog(value = "工种管理-编辑")
    @ApiOperation(value = "工种管理-编辑", notes = "工种管理-编辑")
    @PutMapping(value = "/edit")
    public Result<MesWorkType> edit(@RequestBody MesWorkType mesWorkType) {
        return mesWorkTypeService.updateWorkType(mesWorkType);
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "工种管理-通过id删除")
    @ApiOperation(value = "工种管理-通过id删除", notes = "工种管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        return mesWorkTypeService.removeWorkType(id);
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "工种管理-批量删除")
    @ApiOperation(value = "工种管理-批量删除", notes = "工种管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return mesWorkTypeService.removeWorkTypes(idList);
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "工种管理-通过id查询")
    @ApiOperation(value = "工种管理-通过id查询", notes = "工种管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MesWorkType> queryById(@RequestParam(name = "id", required = true) String id) {
        MesWorkType mesWorkType = mesWorkTypeService.getById(id);
        if (mesWorkType == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(mesWorkType);
    }

    /**
     * 获取所有工种类型
     */
    @AutoLog(value = "工种管理-获取所有工种类型")
    @ApiOperation(value = "工种管理-获取所有工种类型", notes = "工种管理-获取所有工种类型")
    @GetMapping(value = "/all")
    public Result<List<MesWorkType>> getAllWorkTypes() {
        List<MesWorkType> workTypes = mesWorkTypeService.getAllWorkTypes();
        return Result.OK(workTypes);
    }

    /**
     * 检查工种编码是否存在
     */
    @AutoLog(value = "工种管理-检查工种编码是否存在")
    @ApiOperation(value = "工种管理-检查工种编码是否存在", notes = "工种管理-检查工种编码是否存在")
    @GetMapping(value = "/checkWorkTypeCode")
    public Result<Boolean> checkWorkTypeCode(@RequestParam(name = "workTypeCode", required = true) String workTypeCode,
                                           @RequestParam(name = "excludeId", required = false) String excludeId) {
        boolean exists = mesWorkTypeService.checkWorkTypeCodeExists(workTypeCode, excludeId);
        return Result.OK(exists);
    }

    /**
     * 获取工种下的员工列表
     */
    @AutoLog(value = "工种管理-获取工种下的员工列表")
    @ApiOperation(value = "工种管理-获取工种下的员工列表", notes = "工种管理-获取工种下的员工列表")
    @GetMapping(value = "/employees/{workTypeId}")
    public Result<List<Map<String, Object>>> getWorkTypeEmployees(@PathVariable String workTypeId) {
        List<Map<String, Object>> employees = mesWorkTypeService.getWorkTypeEmployees(workTypeId);
        return Result.OK(employees);
    }

    /**
     * 为工种添加员工
     */
    @AutoLog(value = "工种管理-为工种添加员工")
    @ApiOperation(value = "工种管理-为工种添加员工", notes = "工种管理-为工种添加员工")
    @PostMapping(value = "/add-employees")
    public Result<?> addEmployeesToWorkType(@RequestBody Map<String, Object> requestData) {
        return mesWorkTypeService.addEmployeesToWorkType(requestData);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MesWorkType mesWorkType) {
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        
        // 添加查询条件
        params.put("workTypeCode", request.getParameter("workTypeCode_like"));
        params.put("workTypeName", request.getParameter("workTypeName_like"));
        params.put("status", request.getParameter("status"));
        
        // 调用Service查询所有数据
        Page<MesWorkType> page = new Page<>(1, Integer.MAX_VALUE);
        IPage<MesWorkType> pageList = mesWorkTypeService.queryPageList(page, params);
        List<MesWorkType> list = pageList.getRecords();

        return super.exportXlsByResult(request, MesWorkType.class, "工种管理", list);
    }
}
